package geekbang.week1.submit;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.util.Base64;
/**
 * @author 张仕强
 * version 1.0
 * @date 2021/6/26 20:41
 */
public class Task2 extends ClassLoader {
    public static  void main(String[] args) {
        try {
            Object testo;

            Class helloc= new Task2().findClass("Hello");
            testo=helloc.newInstance();
            Method m=helloc.getMethod("hello",new Class[]{});
            m.invoke(testo);

        } catch (ClassNotFoundException e)  {
            e.printStackTrace();
        } catch (NoSuchMethodException e)   {
            e.printStackTrace();
        } catch (IllegalAccessException e)  {
            e.printStackTrace();
        } catch (InvocationTargetException e)  {
            e.printStackTrace();
        } catch (InstantiationException e)  {
            e.printStackTrace();
        }
    }

    @Override
    protected Class<?> findClass(String name) throws ClassNotFoundException {
        String helloBase64 ="NQFFQf///8v/4/X/+f/x9v/w/+/3/+71/+3/7Pj/6/j/6v7/+cOWkZaLwf7//NfWqf7/+7yQm5r+"+
                "//CzlpGasYqSnZqNq56dk5r+//qXmpOTkP7/9ayQio2cmrmWk5r+//W3mpOTkNGVnome8//4//f4"+
                "/+nz/+j/5/7/7Leak5OQ09+ck56MjLOQnpuajd74/+bz/+X/5P7/+reak5OQ/v/vlZ6JntCTnpGY"+
                "0LCdlZqci/7/75WeiZ7Qk56RmNCshoyLmpL+//yQiov+/+qzlZ6JntCWkNCvjZaRi6yLjZqeksT+"+
                "/+yVnome0JaQ0K+NlpGLrIuNmp6S/v/4j42WkYuTkf7/6tezlZ6JntCTnpGY0KyLjZaRmMTWqf/e"+
                "//r/+f///////f/+//j/9//+//b////i//7//v////rVSP/+Tv////7/9f////n//v////7//v/0"+
                "//f//v/2////2v/9//7////2Tf/97fxJ//tO/////v/1////9f/9////+//3//r//v/z/////f/y";

        byte[] bytes=getOrgByte(decode(helloBase64));
        return defineClass(name,bytes,0,bytes.length);
    }
    public byte[] getOrgByte(byte[] bytes)    {
        byte factor = (byte)0xFF;
        byte[] orgByte=new byte[bytes.length];
        for(int i=0;i<bytes.length;i++) {
            orgByte[i]=(byte) (factor-bytes[i]);
        }
        return orgByte;
    }
    public byte[] decode(String base64){
        return Base64.getDecoder().decode(base64);
    }
}
